#!/usr/bin/env node

const puppeteer = require('puppeteer');
const log = require('./libs/log.cjs');
const {promisePool} = require('./libs/db.cjs');


let page;

(async () => {
    const browser = await puppeteer.launch({
        headless: false, // 设置为 false 以查看浏览器操作过程
        defaultViewport: null, // 取消默认视口限制
        args: ['--no-sandbox', '--disable-setuid-sandbox', '--start-maximized'],
    });

    const pages = await browser.pages();
    if (pages.length > 0) {
        page = pages[0];
    } else {
        page = await browser.newPage();
    }
    await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36');
    await page.setRequestInterception(true);

    page.on('request', (request) => {
        // 继续所有请求
        request.continue();
    });

    async function navToPage() {

        await page.evaluate(() => {
            window.scrollTo(0, document.body.scrollHeight); // 滚到底部
        });

        // await page.click('input.search-pagination-to-page-input--NDqqDgSl', 2);
        const buttons = await page.$$('button.search-pagination-arrow-container--lt2kCP6J');
        log.info(buttons.length);
        if (buttons.length === 2) {
            const button = buttons[1];
            await button.evaluate(el => el.scrollIntoView({behavior: 'smooth', block: 'center'}));
            await new Promise(resolve => setTimeout(resolve, 3000)); // 等滚动完
            await button.click();
        }
    }

    page.on('response', async (response) => {
        const url = response.url();
        if (url.includes('/mtop.taobao.idlemtopsearch.pc.search/')) {
            const contentType = response.headers()['content-type'];
            if (['application/json', 'application/x-www-form-urlencoded', 'text/plain'].some(type => contentType && contentType.includes(type))) {
                const respData = await response.json();
                if (respData && respData.data && Array.isArray(respData.data.resultList)) {
                    respData.data.resultList.map(async ({data}) => {
                        const {
                            item: {
                                main: {
                                    clickParam: {
                                        args: {
                                            publishTime,
                                            item_id,
                                            price,
                                            tagname: ownerItemCondition,
                                        }
                                    },
                                    exContent: {
                                        area,
                                        fishTags,
                                        picUrl,
                                        title
                                    }
                                }
                            }
                        } = data;
                        const grades = [];
                        const brands = [];
                        const tags = [];
                        const tagNameDict = {
                            '279': '倍数'
                        };
                        if (fishTags) {
                            for (const key in fishTags) {
                                const item = fishTags[key];
                                if (item?.tagList) {
                                    item.tagList.map(({data: {labelId, content}}) => {
                                        if (labelId === '36') {
                                            grades.push(content);
                                        } else if (labelId === '38') {
                                            brands.push(content);
                                        } else {
                                            const tagName = tagNameDict[labelId];
                                            const kv = {};
                                            if (tagName) {
                                                kv[tagName] = content
                                            } else {
                                                kv[labelId] = content
                                            }
                                            tags.push(kv);
                                        }
                                    });
                                }
                            }
                        }

                        const kv = {
                            item_id,
                            area,
                            title,
                            price,
                            publish_ts: Math.ceil(publishTime / 1000),
                            grade: grades.join('|'),
                            brand: brands.join('|'),
                            tags: JSON.stringify(tags),
                            pic_url: picUrl,
                        };

                        const columns = [];
                        const values = [];
                        const placeholders = [];
                        for (const k in kv) {
                            const v = kv[k];
                            columns.push('`' + k + '`');
                            values.push(v);
                            placeholders.push('?');
                        }
                        try {
                            const sql = 'INSERT INTO `fish_goods` (' + columns.join(',') + ') VALUES (' + placeholders.join(',') + ')';
                            const [result] = await promisePool.execute(sql, values);
                            log.info(JSON.stringify(result));

                            await navToPage();
                        } catch (e) {
                            log.error(e);
                        }
                    });
                }
            }
        }
    });

    // 导航到你想要监听的页面
    await page.goto('https://www.goofish.com/');
    await page.setViewport({width: 1920, height: 888});
    await page.waitForNetworkIdle();

    const inputs = await page.evaluate(() => {
        // 返回所有 input 元素的信息
        return Array.from(document.querySelectorAll('input')).map(element => ({
            className: element.className
        }));
    });
    const searchButtons = await page.evaluate(() => {
        // 返回所有 input 元素的信息
        return Array.from(document.querySelectorAll('button[type=submit]')).map(element => ({
            className: element.className
        }));
    });

    let inputClassName = '';
    for (let i = 0; i < inputs.length; i++) {
        const input = inputs[i];
        if (input.className.indexOf('search-input--') > -1) {
            inputClassName = input.className;
        }
    }

    let searchButtonClassName = '';
    for (let i = 0; i < searchButtons.length; i++) {
        const searchButton = searchButtons[i];
        if (searchButton.className.indexOf('search-icon--') > -1) {
            searchButtonClassName = searchButton.className;
        }
    }

    await page.type(`input.${inputClassName}`, 'Pentax 镜头');
    await page.click(`button.${searchButtonClassName}`);

    await page.waitForNetworkIdle();
    // await autoScroll(page);

    const pageElements = await page.$$('.search-pagination-page-box--AbqmJFFp');
    const ptn = new RegExp('^\\d+$');
    let maxPage = 0;
    for (let i = 0; i < pageElements.length; i++) {
        const pageElement = pageElements[i];
        console.log(`[LOG] pageElement`, pageElement);
        if (pageElement) {
            const text = await pageElement.evaluate(el => el.textContent);
            if (ptn.test(text)) {
                if (parseInt(text) > maxPage) {
                    maxPage = parseInt(text);
                }
            }
        }
    }

    // await autoScroll(page);
    // await page.screenshot({
    //     path: '1.png',
    //     fullPage: true
    // });
    // await browser.close();
})();